/**
 * @author banglong
 * @date 2016/11/8
 */
define([], function () {

    // dh 单据需要保存的信息
    function getDHDrawBill(goods, newBillVm, Openparm, runtimeParam) {
        var drawBill = Object.create(null);
        var drawBillHead = Object.create(null);
        drawBillHead.ApplyDeptAccount = newBillVm.InDeptAccountID;
        drawBillHead.ApplyDeptID = newBillVm.InDeptAccountCode;
        drawBillHead.OutDeptAccountID = newBillVm.OutDeptAccountID;
        drawBillHead.OutDeptID = newBillVm.OutDeptAccountCode;
        drawBillHead.BillSN = newBillVm.bill.BillSN || null;
        drawBillHead.InputUserID = runtimeParam.user.Ygid;
        drawBillHead.BigCatID = Openparm.PackageBigCat;
        drawBillHead.BillType = newBillVm.BillType;
        drawBillHead.BillTypeStr = newBillVm.BillType;
        drawBill.Head = drawBillHead;

        var drawBillDetails = [];
        for (var i = 0; i < goods.length; i++) {
            var detail = Object.create(null);
            var good = goods[i];
            detail.ActualCount = good.ActualCount || '0';
            detail.BillSN = good.BillSN || null;
            detail.Counts = good.ApplyNum || null;
            detail.BillDetailSN = good.DetailSN || null;
            detail.PackageID = good.PackageID || good.GoodsID;
            detail.Sort = i + 1;
            drawBillDetails.push(detail);
        }
        drawBill.Details = drawBillDetails;
        return drawBill;
    }

    // 器械消毒单
    function getQXXDBill(goods, newBillVm, Openparm, runtimeParam) {
        var drawBill = Object.create(null);
        var drawBillHead = Object.create(null);

        drawBillHead.BillSN = newBillVm.bill.BillSN || null;
        drawBillHead.ApplyDeptAccountID = runtimeParam.CurrentDeptAccount.DeptAccountID;
        drawBillHead.DeptID = runtimeParam.CurrentDeptAccount.Ksdm;
        drawBillHead.BigCatID = Openparm.PackageBigCat;
        drawBillHead.InputUserID = newBillVm.bill.InputUserID || runtimeParam.user.Ygid;
        drawBillHead.SendUserID = newBillVm.bill.SendUserID || null;
        drawBillHead.DisinfectUserID = newBillVm.bill.DisinfectUserID || null;
        drawBillHead.SendInstrumentUserID = newBillVm.bill.SendInstrumentUserID || null;
        drawBillHead.InputTime = newBillVm.bill.InputTime || null;
        drawBillHead.SendTime = newBillVm.bill.SendTime || null;
        drawBillHead.DisinfectTime = newBillVm.bill.DisinfectTime || null;
        drawBillHead.SendInstrumentTime = newBillVm.bill.SendInstrumentTime || null;
        drawBillHead.Status = newBillVm.bill.Status || null;
        drawBillHead.StatusStr = newBillVm.bill.StatusStr || null;
        drawBill.Head = drawBillHead;

        var drawBillDetails = [];
        for (var i = 0; i < goods.length; i++) {
            var detail = Object.create(null);
            var good = goods[i];
            detail.BillDetailSN = good.DetailSN || null;
            detail.BillSN = good.BillSN || null;
            detail.GoodsID = good.GoodsID || null;
            detail.ApplyCounts = good.ApplyNum || 1;
            detail.ActualCounts = good.ActualCounts || good.ApplyNum;
            detail.ActualSendCounts = good.ActualSendCounts || good.ApplyNum;
            drawBillDetails.push(detail);
        }
        drawBill.Details = drawBillDetails;
        return drawBill;
    }

    function getApplyBill(goods, newBillVm, Openparm, runtimeParam) {
        var drawBill = Object.create(null);
        var drawBillHead = Object.create(null);

        drawBillHead.lsh = newBillVm.bill.BillSN || null;
        drawBillHead.DeptID = runtimeParam.CurrentDeptAccount.Ksdm;
        drawBillHead.sqrid = newBillVm.bill.InputUserID || runtimeParam.user.Ygid;
        drawBillHead.sqsj = new Date().format('yyyy-MM-dd hh:mm:ss');
        drawBillHead.fsrid = null;
        drawBillHead.fssj = null;
        drawBillHead.hzrid = null;
        drawBillHead.hzsj = null;
        drawBillHead.zfrid = null;
        drawBillHead.zfsj = null;
        drawBillHead.zt = 1;
        drawBill.Head = drawBillHead;

        var drawBillDetails = [];
        for (var i = 0; i < goods.length; i++) {
            var detail = Object.create(null);
            var good = goods[i];
            detail.mxlsh = null;
            detail.lsh = null;
            detail.GoodsID = good.GoodsID || null;
            detail.sqsl = good.ApplyNum || 1;
            detail.sjsqsl = good.ApplyNum || 1;
            detail.bz = good.bz;
            drawBillDetails.push(detail);
        }
        drawBill.Details = drawBillDetails;
        return drawBill;
    }

    // 通用单据
    function getCommonBill(goods, newBillVm, Openparm, runtimeParam) {
        var drawBill = Object.create(null);
        var drawBillHead = {};
        drawBillHead.InDeptAccountID = newBillVm.InDeptAccountID;
        drawBillHead.InDeptCode = newBillVm.InDeptAccountCode;
        drawBillHead.OutDeptAccountID = newBillVm.OutDeptAccountID;
        drawBillHead.OutDeptCode = newBillVm.OutDeptAccountCode;
        drawBillHead.PrintTimes = 0;
        drawBillHead.Comment = newBillVm.bill.Comment;
        drawBill.Head = drawBillHead;

        if (newBillVm.isGYKS) {
            drawBill.Head.BigCatID = Openparm.PackageBigCat;
            drawBill.Head.BillType = 'ly';
            drawBill.Head.BillTypeStr = 'ly';
        } else {
            drawBill.Head.GoodsBookID = Openparm.GoodsBook;
            drawBill.Head.MoveBillReasonType = 'zc';
            drawBill.Head.MoveBillReasonTypeStr = 'zc';
        }

        // 处理单据明细信息
        var drawBillDetails = [];
        for (var i = 0; i < goods.length; i++) {
            var detail = {};
            var good = goods[i];

            detail.BillDetailSN = good.BillDetailSN || '';
            detail.BillSN = good.BillSN || '';
            detail.GoodsID = good.GoodsID || '';
            detail.ApplyNum = good.ApplyNum || '';
            detail.ActualSendNum = good.ApplyNum;
            detail.RefPrice = good.RefPrice || 0;
            detail.Sort = i + 1;
            detail.ActualSendNum = good.ApplyNum;

            if (!newBillVm.isGYKS) {
                detail.CardNumberList = good.CardNumberList || '';
                detail.HasCard = good.HasCard || false;
                detail.Comment = good.Comment || '';
            }
            drawBillDetails.push(detail);
        }
        drawBill.Details = drawBillDetails;
        return drawBill;
    }

    // 创建一个新单据的信息
    var getDrawBill = function (goods, newBillVm, Openparm, runtimeParam) {

        // 保存单据
        var drawBill = Object.create(null);
        switch (newBillVm.BillType) {
            case 'dh':
            case 'jy':
                drawBill = getDHDrawBill(goods, newBillVm, Openparm, runtimeParam);
                break;
            case 'qxxd':
                drawBill = getQXXDBill(goods, newBillVm, Openparm, runtimeParam);
                break;
            case 'sq':
                drawBill = getApplyBill(goods, newBillVm, Openparm, runtimeParam);
                break;
            case 'ygsq':
                drawBill = getApplyBill(goods, newBillVm, Openparm, runtimeParam);
                break;
            default:
                drawBill = getCommonBill(goods, newBillVm, Openparm, runtimeParam);
                break;
        }

        if (runtimeParam.BillStatus !== 'new') { //修改单据
            drawBill.Head.BillSN = runtimeParam.bill.BillSN;
            drawBill.Head.InputTime = new Date(runtimeParam.bill.InputTime).format('yyyy-MM-dd hh:mm:ss');
            drawBill.Head.InputUserID = runtimeParam.bill.InputUserID;
            drawBill.Head.Status = runtimeParam.bill.Status;
            drawBill.Head.StatusStr = runtimeParam.bill.StatusStr;
        }
        return drawBill;
    };

    return {
        getDrawBill: getDrawBill
    }
});